home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bc_ti.zip / TI397.ASC < prev    next >
Text File  |  1992-02-25  |  7KB  |  331 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  TURBO C                                NUMBER  :  397
  9.   VERSION  :  1.0
  10.        OS  :  PC-DOS
  11.      DATE  :  November 20, 1987                        PAGE  :  1/5
  12.  
  13.   TITLE  :  EXPANDING COMMAND LINE WILDCARDS
  14.  
  15.  
  16.  
  17.  
  18.   It is sometimes desirable to expand the DOS wildcards '*' and '?'
  19.   used in directory  searches.   The Turbo C functions, "findfirst"
  20.   and "findnext", accept wildcards  in  pathnames  and are the most
  21.   straightforward way to search a  directory.  Following is a brief
  22.   description of each:
  23.  
  24.   int findfirst (char *pathname, struct ffblk *f, int attrib);
  25.  
  26.   findfirst  accepts  a  string for the  path  or  filename  to  be
  27.   searched and a file attribute to search for (0 =  exists).   If a
  28.   file is found which matches  the  requirements,  findfirst enters
  29.   the information for that file into the ffblk variable (defined in
  30.   dir.h) and returns 0.  If an error occurs, or  no  file is found,
  31.   -1 is returned and the global variable errno is set to one of the
  32.   following:
  33.  
  34.             ENOENT   path or filename not found.
  35.             ENMFILE  no more files.
  36.  
  37.   int findnext (struct ffblk *f);
  38.  
  39.   findnext is used to fetch subsequent files which  match  the path
  40.   or filename and attribute given in findfirst.  The ffblk variable
  41.   is the same block filled in by findfirst.   The  values  returned
  42.   are the same.
  43.  
  44.   These functions require the dir.h file to be included.
  45.  
  46.   The following function, _setargv, illustrates  the  use  of these
  47.   functions.  It  replaces  the standard runtime library routine of
  48.   the same name and can be linked into existing code which uses the
  49.   standard arguments argv and argc.  To do so, first compile  to an
  50.   .OBJ file.  This can be  done  either by selecting the compile to
  51.   .OBJ option  from the compile menu in the Integrated Environment,
  52.   or by typing the following at the command line:
  53.  
  54.   tcc -mX -c -I\turboc\include -L\turboc\lib setargv.c
  55.  
  56.   where X is the memory model you are using.   The  object file can
  57.   then be linked with a main file (also compiled to an  .obj), with
  58.   the following command:
  59.  
  60.   tlink c0X mainprog setargv, mainprog,, emu mathX cX
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  TURBO C                                NUMBER  :  397
  75.   VERSION  :  1.0
  76.        OS  :  PC-DOS
  77.      DATE  :  November 20, 1987                        PAGE  :  2/5
  78.  
  79.   TITLE  :  EXPANDING COMMAND LINE WILDCARDS
  80.  
  81.  
  82.  
  83.  
  84.                           or:
  85.  
  86.   tcc -mX -I\turboc\include -L\turboc\lib mainprog.c setargv.obj
  87.  
  88.   The same result  can  be achieved with the Integrated Environment
  89.   using the following .PRJ file:
  90.  
  91.   mainprog
  92.   setargv.obj
  93.  
  94.   Following is the code for _setargv:
  95.  
  96.   /*
  97.    *  SETARGV.C
  98.    */
  99.  
  100.   #include <dir.h>
  101.   #include <dos.h>
  102.   #include <string.h>
  103.   #include <alloc.h>
  104.  
  105.   extern unsigned _psp;    /* psp segment */
  106.   extern int __argc;       /* argc */
  107.   extern char **__argv;    /* argv */
  108.   extern unsigned _envseg; /* environment segment */
  109.   extern unsigned _envLng; /* program name offset in environment */
  110.  
  111.   /*--------------------------------------------------------------
  112.    * _setargv - a replacement for the _setargv in cX.lib
  113.    *            this version expands commandline wildcards
  114.    *------------------------------------------------------------*/
  115.   void _setargv()
  116.   {
  117.     unsigned pspcmd = 0x0081;  /* command line offset in psp. */
  118.     char far *cmdline;         /* pointer to the psp command line */
  119.     char far *startp;          /* begining of a command line argument */
  120.     char far *endp;            /* end of a command line argument */
  121.     char *s;
  122.     int v = 1,
  123.         is_wildcard = 0,
  124.            foundall, i = 0,
  125.            done = 0;
  126.     struct ffblk fblk;         /* structure used by findfirst/findnext */
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  TURBO C                                NUMBER  :  397
  141.   VERSION  :  1.0
  142.        OS  :  PC-DOS
  143.      DATE  :  November 20, 1987                        PAGE  :  3/5
  144.  
  145.   TITLE  :  EXPANDING COMMAND LINE WILDCARDS
  146.  
  147.  
  148.  
  149.  
  150.     __argc = 1;
  151.     cmdline = (char far *) MK_FP (_psp,pspcmd);
  152.     while (*cmdline == 32) ++cmdline;/* skip spaces */
  153.     startp = cmdline;
  154.     done = *cmdline == 13;
  155.  
  156.     while (!done)              /* get value for argc */
  157.     {
  158.       endp = startp + 1;
  159.       while (32 < *endp) ++endp;
  160.       if (*endp == 32)
  161.         *endp = '\0';  /* replace space with null */
  162.       s = (char *) malloc ((1 + endp - startp) * sizeof(char));
  163.       i = 0;
  164.       while (startp <= endp)
  165.       {
  166.         if ((*startp == '*') || (*startp == '?'))
  167.           is_wildcard = 1;
  168.         s[i++] = *startp++;
  169.       }
  170.       if (*endp == 13)
  171.         s[i - 1] = '\0';
  172.       if (is_wildcard) /* expand wild cards */
  173.       {
  174.         i = 0;
  175.         foundall = findfirst(s,&fblk,0);
  176.         while (!foundall)
  177.         {
  178.           ++__argc;
  179.           foundall = findnext(&fblk);
  180.         }
  181.         is_wildcard = 0;
  182.       }
  183.       else ++__argc;
  184.       free (s);
  185.       if (*endp == 13)
  186.         done = 1;
  187.       else
  188.       {
  189.         while (*startp == 32) ++startp;
  190.         done = *startp == 13;
  191.       }
  192.     }
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  TURBO C                                NUMBER  :  397
  207.   VERSION  :  1.0
  208.        OS  :  PC-DOS
  209.      DATE  :  November 20, 1987                        PAGE  :  4/5
  210.  
  211.   TITLE  :  EXPANDING COMMAND LINE WILDCARDS
  212.  
  213.  
  214.  
  215.  
  216.     /*
  217.        allocate space for pointers in argv
  218.     */
  219.  
  220.     __argv = (char **) malloc (__argc * sizeof(char *));
  221.     done = __argc == 1;
  222.     startp = cmdline;
  223.  
  224.     while (!done)             /* copy arguments into argv */
  225.     {
  226.       endp = startp + 1;
  227.       while (*endp > 32) ++endp;
  228.       s = (char *) malloc ((1 + endp - startp) * sizeof(char));
  229.       i = 0;
  230.       is_wildcard = 0;
  231.       while (startp <= endp)
  232.       {
  233.         if ((*startp == '*') || (*startp == '?'))
  234.           is_wildcard = 1;
  235.         s[i++] = *startp++;
  236.       }
  237.       if (*endp == 13) s[i - 1] = '\0';
  238.       if (is_wildcard)
  239.       {
  240.         foundall = findfirst (s, &fblk, 0);
  241.         while (!foundall)
  242.         {
  243.           __argv[v++] = strdup (fblk.ff_name);
  244.           foundall = findnext (&fblk);
  245.         }
  246.         free(s);
  247.       }
  248.       else __argv[v++] = strupr (s);
  249.       if (*endp == 13)
  250.         done = 1;
  251.       else
  252.       {
  253.         while (*startp == 32)
  254.           ++startp;
  255.         done = *startp == 13;
  256.       }
  257.     }
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  TURBO C                                NUMBER  :  397
  273.   VERSION  :  1.0
  274.        OS  :  PC-DOS
  275.      DATE  :  November 20, 1987                        PAGE  :  5/5
  276.  
  277.   TITLE  :  EXPANDING COMMAND LINE WILDCARDS
  278.  
  279.  
  280.  
  281.  
  282.     /*
  283.       copy program name into argv[0]
  284.     */
  285.  
  286.     startp = (char far *) MK_FP(_envseg, _envLng + 2);
  287.     endp = startp + 1;
  288.     while (*endp++ != '\0');
  289.     __argv[0] = (char *) malloc ((endp - startp) * sizeof(char));
  290.     v = 0;
  291.     while (startp != endp)
  292.       __argv[0][v++] = *startp++;
  293.   }
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.